<?php
namespace app\index\controller;
use think\Controller;
/**
 * 
 * 猫酷开放平台接口
 * @author 刘宗超 2018-04-20
 *
 */

class MallcooApi extends Controller
{
	public $m_Mallid = '';
    public $m_AppID = '';
    public $m_PublicKey = '';
    public $m_PrivateKey = '';
    /**
     * 初始化
     *
     * @param string $sMallid Mallid
     * @param string $sAppID AppID
     * @param string $sPublicKey 公钥
     * @param string $sPrivateKey 私钥
     * 
     */
    public function __construct(){
        $this->m_Mallid = config('conf.m_mallid');
        $this->m_AppID = config('conf.m_appid');
        $this->m_PublicKey = config('conf.m_publickey');
        $this->m_PrivateKey = config('conf.m_privatekey');
    }
    /**
     * 获取授权url
     *
     * @param string $sCallbackUrl 回调url
     * @return string
     */
    public function getOauthUrl($sCallbackUrl){
        $sUrl = 'https://m.mallcoo.cn/a/open/User/V2/OAuth/BaseInfo/';
        $sUrl .= '?AppID='.$this->m_AppID.'&PublicKey='.$this->m_PublicKey.'&CallbackUrl='.urlencode($sCallbackUrl);
        return $sUrl;
    }

    /**
     * 获取授权url
     *
     * @param string $sCallbackUrl 回调url
     * @return string
     */
    public function getOauthCardUrl($sCallbackUrl){
        $sUrl = 'https://m.mallcoo.cn/a/open/User/V2/OAuth/CardInfo/';
        $sUrl .= '?AppID='.$this->m_AppID.'&PublicKey='.$this->m_PublicKey.'&CallbackUrl='.urlencode($sCallbackUrl);
        return $sUrl;
    }

    /**
     * 通过 Ticket 获取 Token
     *
     * @param string $sTicket Ticket
     * @return
     */
    public function GetTokenByTicket($sTicket){
        $sUrl = 'https://openapi10.mallcoo.cn/User/OAuth/v1/GetToken/ByTicket/';
        return $this->mallcooPost($sUrl, array('Ticket' => $sTicket));
    }

    /**
     * 通过 UserToken 获取用户信息
     *
     * @param string $sUserToken UserToken
     * @return
     */
    public function GetUserInfoByUserToken($sUserToken){
        $sUrl = 'https://openapi10.mallcoo.cn/User/BaseInfo/v1/Get/ByToken/';
        return $this->mallcooPost($sUrl, array('UserToken' => $sUserToken));
    }

    /**
     * 通过 UserId 获取会员信息
     *
     * @param string $sUserId OpenUserId
     * @return
     */
    public function GetCardInfoByUserId($sUserId){
        $sUrl = 'https://openapi10.mallcoo.cn/User/AdvancedInfo/v1/Get/ByOpenUserID/';
        return $this->mallcooPost($sUrl, array('OpenUserId' => $sUserId));
    }
    
    /**
     * 通过 手机号 phone 获取会员信息
     *
     * @param string $sUserId OpenUserId
     * @return
     */
    public function GetCardInfoByMobile($Mobile){
        $sUrl = 'https://openapi10.mallcoo.cn/User/AdvancedInfo/v1/Get/ByMobile/';
        return $this->mallcooPost($sUrl, array('Mobile' => $Mobile));
    }

    /**
     * mallcoo post 请求
     *
     * @param string $sUrl 请求url
     * @param array $aPostData 请求参数
     * @return array
     */
    private function mallcooPost($sUrl, $aPostData){
        $sPostData = json_encode($aPostData);
        trace($sPostData,"debug");
        $nTimeStamp = date('YmdHis',time());
        $sS = "{publicKey:".$this->m_PublicKey.",timeStamp:".$nTimeStamp.",data:".$sPostData.",privateKey:".$this->m_PrivateKey."}";
        $sSign = strtoupper(substr(md5($sS), 8, 16));
        $aHeader = array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($sPostData),
            'AppID: '.$this->m_AppID,
            'TimeStamp: '.$nTimeStamp,
            'PublicKey: '.$this->m_PublicKey,
            'Sign: '.$sSign,
        );
        $sR = $this->curl_post($sUrl, $aHeader, $sPostData);
        trace($sR,"debug");
        return json_decode(html_entity_decode($sR), true);
    }
    /**
     * curl post 请求
     * @param string $url
     * @param array $aHeader
     * @param string $sParams
     * @param string $cookie
     * @return string
     */
    private function curl_post($url, $aHeader, $sParams, $cookie='') {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $sParams);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $result = curl_exec($ch);
        if ($result === false){
            // log curl_error($ch);
        }
        curl_close($ch);
        return $result;
    }

}
